package com.zlj.request;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * request获取请求路径相关的方法
 * 
 * 获取请求的URL:http://localhost:8080/servletDemo/RequestBServlet?username=zhangsan&password=123456
 * 
 * String getScheme():获取协议 http
 * String getServerName():获取服务器名 localhost
 * String getServerPort():获取服务器端口 8080
 * String getContextPath():获取项目名  /servletDemo
 * String getServletPath():获取Servlet路径 /RequestBServlet
 * String getQueryString():获取参数部分，即问号后面的部分 username=zhangsan&password=123456
 * String getRequestURI():获取请求URI:等于项目名+servlet路径  /servletDemo/RequestBServlet
 * String getRequestURL():获取请求URL:等于不包含参数的整个请求路径 http://localhost:8080/servletDemo/RequestBServlet
 */
@WebServlet("/RequestBServlet")
public class RequestBServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 通过Request来获取URL的相关方法
		 */
          response.getWriter().println(request.getScheme());  //http
          response.getWriter().println(request.getServerName()); //localhost
          response.getWriter().println(request.getServerPort()); //8080
          response.getWriter().println(request.getContextPath()); //项目名：  /servletDemo
          response.getWriter().println(request.getServletPath()); //servlet路径：  /RequestBServlet 
          response.getWriter().println(request.getQueryString()); //?后面的参数：   username=zhangsan
          response.getWriter().println(request.getRequestURI()); //项目名+servlet路径：  /servletDemo/RequestBServlet
          response.getWriter().println(request.getRequestURL()); //  http://localhost:8080/servletDemo/RequestBServlet
	}
}
